#include "StdAfx.h"
#include "DropEx.h"
#include "logproc.h"
#include "..\include\readscript.h"
#include "DSProtocol.h"
#include "gObjMonster.h"
#include "GameMain.h"

DropEx	g_DropEx;

DropEx::DropEx()
{
	this->Init();
}

DropEx::~DropEx()
{
}

void DropEx::Init()
{
	this->m_Data.clear();
}

void DropEx::Load()
{
	this->ReadData(gDirPath.GetNewPath("Custom\\ItemDropEx.txt"));
}

void DropEx::ReadData(char * File)
{
	this->Init();

	__try
	{
		SMDFile = fopen(File, "r");

		while (true)
		{
			SMDToken Token = GetToken();

			if (Token == END)
			{
				break;
			}

			DROPEX_DATA List;
			List.ItemType			= TokenNumber;
			GetToken();
			List.ItemIndex			= TokenNumber;
			GetToken();
			List.ItemMinLevel		= TokenNumber;
			GetToken();
			List.ItemMaxLevel		= TokenNumber;
			GetToken();
			List.ItemMaxOption		= TokenNumber;
			GetToken();
			List.ItemLuck			= TokenNumber;
			GetToken();
			List.ItemSkill			= TokenNumber;
			GetToken();
			List.ItemExcellent		= TokenNumber;
			GetToken();
			List.ItemDur			= TokenNumber;
			GetToken();
			List.ItemDropRate		= TokenNumber;
			GetToken();
			List.MonsterMinLevel	= TokenNumber;
			GetToken();
			List.MonsterMaxLevel	= TokenNumber;
			GetToken();
			List.MonsterMap			= TokenNumber;
			this->m_Data.push_back(List);
		}
	}
	__finally
	{
		if (SMDFile != NULL)
		{
			LogAdd(lMsg.Get(MSGGET(1, 198)), File);
			fclose(SMDFile);
		}

		SMDFile = NULL;
	}
}

bool DropEx::DropItem(LPOBJ lpObj, LPOBJ lpTargetObj)
{
	switch(lpObj->Class)
	{
	case 78:	//-> Golden Goblin
	case 53:	//-> Golden Titan
	case 79:	//-> Golden Dragon
	case 80:	//-> Golden Lizard
	case 82:	//-> Golden Tantalos
	case 502:	//-> Golden Rabbit
	case 493:	//-> Golden Dark Knight
	case 494:	//-> Golden Devil
	case 495:	//-> ???
	case 496:	//-> Golden Crust
	case 497:	//-> Golden Satiros
	case 498:	//-> Golden Twintail
	case 499:	//-> Golden Iron Knight
	case 500:	//-> Golden Neipin
	case 501:	//-> Golden Great Dragon
		{
			return false;
		}
		break;
	}

	for(int i = 0; i < this->m_Data.size(); i++)
	{
		DROPEX_DATA & Drop = this->m_Data[i];

		if (lpObj->Level >= Drop.MonsterMinLevel && lpObj->Level <= Drop.MonsterMaxLevel)
		{
			if (lpObj->MapNumber == Drop.MonsterMap || Drop.MonsterMap == -1)
			{
				if (rand() % 10000 < Drop.ItemDropRate)
				{
					int iLuck	= 0;
					int iSkill	= 0;
					int iOption	= 0;
					int iLevel	= 0;

					if (Drop.ItemLuck == 1)
					{
						if (rand() % 100 < 50)
						{
							iLuck = 1;
						}
					}

					if (Drop.ItemSkill == 1)
					{
						if (rand() % 100 < 50)
						{
							iSkill = 1;
						}
					}

					if (Drop.ItemMaxOption != 0)
					{
						if (rand() % 100 < 50)
						{
							iOption = rand() % Drop.ItemMaxOption + 1;
						}
					}

					if (Drop.ItemMaxLevel == Drop.ItemMinLevel)
					{
						iLevel = Drop.ItemMinLevel;
					}
					else
					{
						iLevel = Drop.ItemMinLevel + (rand() % (Drop.ItemMaxLevel - Drop.ItemMinLevel + 1));
					}

					ItemSerialCreateSend(lpObj->m_Index, lpObj->MapNumber, lpObj->X, lpObj->Y, ITEMGET(Drop.ItemType, Drop.ItemIndex), iLevel, Drop.ItemDur, iOption, iLuck, iSkill, gObjMonsterTopHitDamageUser(lpObj), Drop.ItemExcellent, 0);
					return true;
				}
			}
		}
	}

	return false;
}